home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / snz128s / src / reindex.c < prev    next >
C/C++ Source or Header  |  1994-06-01  |  13KB  |  555 lines

  1. /***************************************************************************
  2.  *                                                                         *
  3.  * REINDEX.C - Reindex an snews news group article file                    *
  4.  *                                                                         *
  5.  *    Written by M.S. Muir September 1992                                  *
  6.  *                                                                         *
  7.  *                                                                         *
  8.  *    EMAIL: malcolm@muir.demon.co.uk                                      *
  9.  *                                                                         *
  10.  *  Released into the public domain in the hope that someone may find it   *
  11.  *  to be of use.                                                          *
  12.  *                                                                         *
  13.  *  Note however that there is NO WARRANTY WHATSOEVER not even an implied  *
  14.  *  warranty of merchantability or fitness for any particular purpose.     *
  15.  *                                                                         *
  16.  *  This programme may be freely copied and/or modified and/or distributed *
  17.  *  under the terms of the GNI General Public Licence, version 1 as        *
  18.  *  published by the Free Software Foundation.                             *
  19.  *                                                                         *
  20.  ***************************************************************************/
  21.  
  22. /*
  23.  * Source Code Control...
  24.  *
  25.  * $Id: REINDEX.C,v 1.2 1994/02/05 18:48:42 gbj Exp user $
  26.  *
  27.  */
  28.  
  29. #include "defs.h"
  30. #include "active.h"
  31. #include "screen.h"
  32. #include "reindex.h"
  33. #ifdef ATARI
  34. #    include "fileops.h"
  35. #    include "st.h"
  36. #endif
  37.  
  38. #ifndef __TURBOC__
  39. #ifndef ATARI
  40. unsigned long farcoreleft(void);
  41. #endif
  42. #endif
  43.  
  44. INFO            my_stuff;
  45.  
  46. #ifdef ATARI
  47.     extern unsigned long _STACK = 32768;
  48. #else
  49.     extern unsigned int _stklen = 16384;
  50.  
  51. #endif
  52.  
  53. static time_t   t;
  54. static long int first, last;
  55. static int      count, num_art;
  56.  
  57. void            main(int argc, char *argv[])
  58. {
  59.  
  60.     char            newsgroup[81];
  61.     char           *fname;
  62.     char           *p;
  63.     ACTIVE         *gp, *head;
  64.     int             ch, junk_flag;
  65.  
  66.     clrscr();
  67.     if (load_stuff()) {
  68.         v_init(my_stuff.directvideo);
  69. #ifdef ATARI
  70.         textcolor(textf); textbackground(textb);
  71. #else
  72.         textcolor(7); textbackground(0);
  73. #endif
  74.         gotoxy(1, 1);
  75.         clreol();
  76.         printf("Demon Internet Simple News reindex utility\n");
  77.         gotoxy(1, 2);
  78.         clreol();
  79.         printf("        Version %d.%02d [build %d]\n\n", rmj, rmm, rup);
  80.         head = load_active_file();
  81.         head = head;                      /* remove error message */
  82. #ifdef ATARI
  83.         if (argc > 1 && (argv[1][0] == '/' || argv[1][0] == '-')) {
  84. #else
  85.         if ((argc > 2) || (argv[1][0] == '/') || (argv[1][0] == '-')) {
  86. #endif
  87.             printf("reindex: usage is reindex [newsgroup].\n");
  88.             gotoxy(1, scr_rows - 1);
  89.             exit(0);
  90.         }
  91.  
  92.         if (argc == 2) {
  93.             strcpy(newsgroup, argv[1]);
  94.             printf("Processing newsgroup %s", newsgroup);
  95.         }
  96.         else {
  97.             p = select_group(head, NULL);
  98.             if (p == NULL) {
  99.                 printf("Reindex: Processing Terminated\n\n");
  100.                 gotoxy(1, scr_rows-1);
  101.                 exit(1);
  102.             }
  103.             else
  104.                 strcpy(newsgroup, p);
  105.         }
  106.         clrscr();
  107.         gp = find_news_group(newsgroup, &junk_flag);
  108.         if ((gp == NULL) || (junk_flag == 1)) {
  109.             printf("reindex: cannot find newsgroup %s\n\n", newsgroup);
  110.             gotoxy(1, scr_rows - 1);
  111.             exit(1);
  112.         }
  113.  
  114.         fname = make_news_group_name(gp->group);
  115.         printf("Newsgroup %s selected\n\n", newsgroup);
  116.         printf("article file identified as %s\n", fname);
  117.         printf("index file identified as   %s.idx\n\n", fname);
  118.         printf("Proceed ? (y/n) ");
  119.         ch = getch();
  120.         putch(ch);
  121.         ch = tolower(ch);
  122.         if (ch != 'y') {
  123.             printf("\nreindex: terminated.\n\n");
  124.             gotoxy(1, scr_rows - 1);
  125.             exit(0);
  126.         }
  127.  
  128.         putch('\n');
  129.         do_index(gp);
  130.         do_active(gp);
  131.         summary(gp);
  132.         close_active_file();
  133.     }
  134.     else {
  135. #ifdef ATARI
  136.         textcolor(textf); textbackground(textb);
  137. #else
  138.         textcolor(7); textbackground(0);
  139. #endif
  140.         clrscr();
  141.         printf("reindex: Couldn't find necessary item in the .rc files.\n");
  142.     }
  143.  
  144. #ifdef ATARI
  145.     textcolor(textf); textbackground(textb);
  146.     printf("\033q");
  147. #else
  148.     textcolor(7); textbackground(0);
  149. #endif
  150.     gotoxy(1, scr_rows - 1);
  151. }
  152.  
  153. void            do_index(ACTIVE * gp)
  154. {
  155.  
  156.     FILE           *afp, *ifp, *nfp;
  157.     char           *aname, iname[80], nname[80], bname[80], abuf[256], ibuf[256];
  158.     char            isubject[512], asubject[512];
  159.     char           *ind, *art, ch;
  160.     unsigned long   ioffset, inumber, itime;
  161.     unsigned long   aoffset, anext, anumber, atime;
  162.     int             flag, flag2, noquery;
  163.  
  164.     aname = make_news_group_name(gp->group);
  165.     aoffset = 0l;
  166.     flag2 = 0l;
  167.     count = 0;
  168.     noquery = FALSE;
  169.     time(&t);
  170.  
  171.     strcpy(iname, aname);
  172.     strcat(iname, ".idx");
  173.     strcpy(nname, aname);
  174.     strcat(nname, ".ndx");
  175.     strcpy(bname, aname);
  176.     strcat(bname, ".bak");
  177.  
  178.     if ((afp = fopen(aname, "rb")) == NULL) {
  179.         printf("reindex: unable to open article file.\n\n");
  180.         gotoxy(1, scr_rows - 1);
  181.         exit(1);
  182.     }
  183.  
  184.     if ((ifp = fopen(iname, "rb")) == NULL) {
  185.         printf("reindex: unable to open index file.\n");
  186.         printf("reindex: creating new index file.\n\n");
  187.     }
  188.  
  189.     if ((nfp = fopen(nname, "wb")) == NULL) {
  190.         printf("reindex: unable to open update file.\n\n");
  191.         gotoxy(1, scr_rows - 1);
  192.         exit(1);
  193.     }
  194.  
  195.     do {
  196.         strcpy(asubject, " (none)\n");
  197.         flag = 0;
  198.  
  199.         do {
  200.             art = fgets(abuf, 255, afp);
  201.             if (strlen(abuf) == 1)
  202.                 flag = 1;
  203.             if ((strnicmp(abuf, "Subject:", 8) == 0) && (flag == 0)) {
  204.                 if ((strlen(abuf) >= 137) && (abuf[136] != '\n')) {
  205.                     abuf[136] = '\n';
  206.                     abuf[137] = '\0';
  207.                 }
  208.                 strcpy(asubject, abuf + 8);
  209.             }
  210.             if (strnicmp(abuf, "@@@@END", 7) == 0) {
  211.                 anext = ftell(afp);
  212.                 break;
  213.             }
  214.         }
  215.         while (art != NULL);
  216.  
  217.         if (art != NULL) {
  218.             if (ifp != NULL)
  219.                 ind = fgets(ibuf, 255, ifp);
  220.             else
  221.                 ind = NULL;
  222.             gotoxy(1, 15);
  223.             clreol();
  224.             if (ind == NULL) {
  225.                 ioffset = inumber = itime = 0l;
  226.                 strcpy(isubject, "reindex: entry invalid");
  227.             }
  228.             else {
  229.                 ioffset = atol(ibuf);
  230.                 inumber = atol(ibuf + 9);
  231.                 itime = atol(ibuf + 18);
  232.                 strcpy(isubject, ibuf + 28);
  233.             }
  234.  
  235.             cprintf("Ind> %08lu %08lu %09lu %-45.45s", ioffset, inumber, itime, isubject);
  236.             if (flag2 == 0) {
  237.                 first = inumber - 1;
  238.                 anumber = inumber;
  239.                 num_art = 0;
  240.                 if (first == -1) {
  241.                     first = 0;
  242.                     anumber = 1;
  243.                 }
  244.                 atime = itime;
  245.                 flag2 = 1;
  246.             }
  247.             gotoxy(1, 17);
  248.             clreol();
  249.             if ((atime == 0l) || (atime < itime))
  250.                 atime = itime;
  251.             cprintf("Art> %08lu %08lu %09lu %-45.45s", aoffset, anumber, atime, asubject);
  252.             if ((ioffset != aoffset) || (inumber != anumber) ||
  253.                 (strcmp(isubject, asubject) != 0) ||
  254.                 (itime < atime)) {
  255.                 gotoxy(1, 19);
  256.                 if (noquery == FALSE) {
  257.                     printf("Index error in this article, correct it ? (y[es]/n[o]/d[on't ask]) ");
  258.                     ch = getch();
  259.                     putch(ch);
  260.                 }
  261.                 else {
  262.                     ch = 'y';
  263.                 }
  264.                 ch = tolower(ch);
  265.                 if (ch == 'd') {
  266.                     noquery = TRUE;
  267.                     ch = 'y';
  268.                 }
  269.                 if (ch == 'y') {
  270.                     count++;
  271.                     if (atime == 0l)
  272.                         atime = t;
  273.                     sprintf(ibuf, "%08lu %08lu %09lu %s", aoffset, anumber, atime, asubject);
  274.                 }
  275.                 gotoxy(1, 19);
  276.                 clreol();
  277.             }
  278.             if (fputs(ibuf, nfp) == EOF) {
  279.                 printf("reindex: index file write error.\n\n");
  280.                 gotoxy(1, scr_rows - 1);
  281.                 exit(2);
  282.             }
  283.  
  284.             aoffset = anext;
  285.             anumber++;
  286.         }
  287.         num_art++;
  288.     }
  289.     while (art != NULL);
  290.  
  291.     last = anumber;
  292.     if (num_art < 2) {
  293.         first = last = gp->hi_num;
  294.         last++;
  295.     }
  296.     fclose(ifp);
  297.     fclose(afp);
  298.     fclose(nfp);
  299.     if (count > 0) {
  300.         unlink(bname);
  301.         if (rename(iname, bname) != 0) {
  302.             printf("reindex: Unable to rename old index file to .bak\n\n");
  303.         }
  304.         if (rename(nname, iname) != 0) {
  305.             printf("reindex: unable to rename new index file to .idx\n\n");
  306.             gotoxy(1, scr_rows - 1);
  307.             exit(4);
  308.         }
  309.  
  310.     }
  311.     else {
  312.         unlink(nname);
  313.     }
  314. }
  315.  
  316. void            do_active(ACTIVE * gp)
  317. {
  318.     --last;
  319.     printf("Low number = %ld, High number = %ld\n\n", first, last);
  320.     gp->lo_num = first;
  321.     gp->hi_num = last;
  322.     update_active_entry(gp);
  323. }
  324.  
  325. void            summary(ACTIVE * gp)
  326. {
  327.  
  328.     int             i;
  329.     char           *name;
  330.  
  331.     for (i = 15; i < scr_rows; i++) {
  332.         gotoxy(1, i);
  333.         clreol();
  334.     }
  335.  
  336.     gotoxy(1, 15);
  337.     printf("reindex: processing completed.\n\n");
  338.     if (count > 0) {
  339.         printf("The following file has been modified:-\n");
  340.         name = make_news_group_name(gp->group);
  341.         printf("        %s.idx\n", name);
  342.         printf("The original file (if found) has been renamed to:-\n");
  343.         printf("        %s.bak\n", name);
  344.         printf("%d changes were made to the index file.\n", count);
  345.         printf("Active file updated.\n");
  346.     }
  347.     else {
  348.         printf("No changes were made to the index file.\n");
  349.         printf("Active file updated.\n");
  350.     }
  351. }
  352.  
  353. char           *select_group(ACTIVE *head, ACTIVE *current)
  354. {
  355.     ACTIVE         *top;                  /* Newsgroup at page top */
  356.     ACTIVE         *this_group;              /* Current Newsgroup     */
  357.     int             exit_code;              /* Why we are exiting    */
  358.     int             ch, i;
  359.  
  360.     this_group = (current == NULL) ? head : current;
  361.  
  362.     top = head;
  363.     exit_code = 0;
  364.  
  365.     textcolor(textf); textbackground(textb);
  366.     clrscr();
  367.     show_groups(&top, this_group, TRUE, head);
  368.  
  369.     while (exit_code == 0) {
  370.  
  371.         ch = getch();
  372.         switch (ch) {
  373.           case 0:
  374.           case 0xE0:
  375.  
  376.             ch = getch();
  377.             switch (ch) {
  378.  
  379.               case UP_ARR:
  380.                 if (this_group->last != NULL)
  381.                     this_group = this_group->last;
  382.                 break;
  383.  
  384.               case DN_ARR:
  385.                 if (this_group->next != NULL)
  386.                     this_group = this_group->next;
  387.                 break;
  388.  
  389.               case PGUP:
  390.                 for(i=0;i< PAGE_LENGTH; i++) {
  391.                     if (this_group->last == NULL) break;
  392.                     this_group = this_group->last;
  393.                 }
  394.                 break;
  395.  
  396.               case PGDN:
  397.                 for (i=0;i<PAGE_LENGTH;i++) {
  398.                     if (this_group->next == NULL) break;
  399.                     this_group = this_group->next;
  400.                 }
  401.                 break;
  402.  
  403.               case HOME:
  404.                 top = this_group = head;
  405.                 show_groups(&top, this_group, TRUE, head);
  406.                 break;
  407.  
  408.               case END:
  409.                 this_group = head;
  410.                 while (this_group->next != NULL)
  411.                     this_group = this_group->next;
  412.                 break;
  413.  
  414.             }
  415.             break;
  416.           case ENTER:
  417.             exit_code = EX_DONE;
  418.             break;
  419.  
  420.           case ESCAPE:
  421.           case 'Q':
  422.           case 'q':
  423.             exit_code = EX_QUIT;
  424.             break;
  425.         }
  426.         if (exit_code == 0)
  427.             show_groups(&top, this_group, FALSE, head);
  428.     }
  429. #ifdef ATARI
  430.     textcolor(textf); textbackground(textb);
  431. #else
  432.     textcolor(7); textbackground(0);
  433. #endif
  434.     clrscr();
  435.  
  436.     if (exit_code == EX_DONE)
  437.         return (this_group->group);
  438.     else
  439.         return ('\0');
  440. }
  441.  
  442. void            show_groups(ACTIVE ** top, ACTIVE * this_group, int force, ACTIVE *head)
  443. {
  444.  
  445.     static         last_y;
  446.     static         last_index;
  447.      int            i;
  448.     ACTIVE        *that;
  449.  
  450.     if (force || ((*top)->index > this_group->index) ||
  451.         (this_group->index - (*top)->index) > PAGE_LENGTH-1) {
  452.  
  453.         clrscr();
  454.         textbackground(headb); textcolor(headf);
  455. #ifdef ATARI
  456.         printf("%*s", scr_cols, " ");
  457.         printf("%*s", scr_cols, " ");
  458.         gotoxy(1, 1);
  459. #else
  460.         clreol();
  461. #endif
  462.         cprintf("        Select Newsgroup  (%s)  [%ldk]   \r\n",
  463.             VERSION, farcoreleft()/1024);
  464. #ifndef ATARI
  465.         clreol();
  466. #else
  467.         gotoxy(1, 2);
  468. #endif
  469.         cprintf("    Reindex");
  470.  
  471.         gotoxy(60,2);
  472.         cprintf("Group %3d of %3d\r\n", last_index+1, head->groups);
  473.         textbackground(textb); textcolor(textf);
  474.         
  475.         /* now adjust the top */
  476.         *top = this_group;
  477.         for (i=0;i<PAGE_LENGTH/2;i++) {
  478.             if ((*top)->last == NULL) break;
  479.             *top = (*top)->last;
  480.         }
  481.  
  482.         that = *top;
  483.  
  484.         for (i = 0; i < PAGE_LENGTH; i++) {
  485.             gotoxy(7, i + 4);
  486.              printf("           %4ld %s", that->hi_num - that->lo_num,
  487.                     that->group);
  488.             that = that->next;
  489.             if (that == NULL)
  490.                 break;
  491.         }
  492.  
  493.         last_y = this_group->index - (*top)->index;
  494.         gotoxy(5, last_y + PAGE_HEADER);
  495.         putch('-');
  496.         putch('>');
  497.         last_index = this_group->index;
  498.     }
  499.     else {
  500.  
  501.         gotoxy(5, last_y + PAGE_HEADER);
  502.         putch(' '); putch(' ');
  503.  
  504.         last_y += (this_group->index - last_index);
  505.         gotoxy(5, last_y + PAGE_HEADER);
  506.         putch('-');
  507.         putch('>');
  508.         last_index = this_group->index;
  509.     }
  510.  
  511.     gotoxy(60,2);
  512.     textbackground(headb); textcolor(headf);
  513.     cprintf("Group %3d of %3d\r\n", last_index+1, head->groups);
  514.     textbackground(textb); textcolor(textf);
  515.  
  516.     command("ESC=quit   ENTER=select group");
  517.  
  518. }
  519.  
  520. void            command(char *msg)
  521. {
  522.     int             x;
  523.  
  524.     gotoxy(1, scr_rows);
  525.     textbackground(msgb);
  526.     textcolor(msgf);
  527.     clreol();
  528.  
  529.     x = (scr_cols - strlen(msg)) / 2;
  530.     gotoxy(x, scr_rows);
  531.     cprintf(msg);
  532.  
  533.     textbackground(textb);
  534.     textcolor(textf);
  535. }
  536.  
  537. #ifndef __TURBOC__
  538. #ifndef ATARI
  539.  
  540. unsigned long farcoreleft(void)
  541. {
  542.  
  543.     union _REGS inregs, outregs;
  544.  
  545.     _heapmin();
  546.     inregs.h.ah = 0x48;
  547.     inregs.x.bx = 0xffff;
  548.     _intdos(&inregs, &outregs);
  549.  
  550.     return (16l * (long)outregs.x.bx);
  551. }
  552.  
  553. #endif
  554. #endif
  555.